package com.sky.alogrithm.chapter01_data_structure;


import java.util.Iterator;

/**
 * 下压(LIFO)栈
 */
public class ResizingArrayStack<T> implements Iterable<T> {

    private T[] a = (T[]) new Object[1];
    private int N = 0;

    public boolean isEmpty(){
        return N == 0;
    }

    public int size(){
        return N;
    }

    private void resize(int max){
        T[] temp = (T[]) new Object[max];
        for(int i = 0; i < N; i++){
            temp[i] = a[i];
        }
        a = temp;
    }

    public void push(T t){
        if(N == a.length){
            resize(2 * a.length);
        }
        a[N++] = t;
    }

    public T pop(){
        T t = a[--N];
        a[N] = null;
        if(N > 0 && N == a.length / 4){
            resize(a.length / 2);
        }
        return t;
    }




    @Override
    public Iterator<T> iterator() {
        return new ReverseArrayIterator();
    }

    private class ReverseArrayIterator implements Iterator<T>{

        private int i = N;

        @Override
        public boolean hasNext() {
            return i > 0;
        }

        @Override
        public T next() {
            return a[--i];
        }

        @Override
        public void remove() {
            //Iterator.super.remove();
        }
    }
}
